package com.aurora_manito.xiaomi.Controller;


import com.aurora_manito.xiaomi.entity.Cart;
import com.aurora_manito.xiaomi.entity.Goods;
import com.aurora_manito.xiaomi.entity.User;
import com.aurora_manito.xiaomi.service.CartService;
import com.aurora_manito.xiaomi.service.GoodsService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Aurora_Manito
 * @since 2020-05-27
 */
@RestController
@RequestMapping("/xiaomi/cart")
public class CartController {
    @Autowired
    CartService cartService;
    @Autowired
    GoodsService goodsService;
    @GetMapping(value = "/addCart", produces = {"application/json;charset=UTF-8"})
    @ResponseBody //序列化--》类型转换  --》jackson ---Json
    public ModelAndView addCart(HttpSession session,@RequestParam String goodsId, @RequestParam String number)
    {
        User user= (User) session.getAttribute("user");
        //判断是否登录
        if(user==null)
        {

            return new ModelAndView("redirect:/register.jsp");
        }
        if(goodsId.isEmpty())
        {
            return  new ModelAndView("redirect:/index.jsp");
        }
        //2.添加到购物车（数据库存储） 查询用户通过用户ID和goodsid
        Cart cart=cartService.findByUidAndgid(user.getId(),Integer.parseInt(goodsId));
        //通过Goodid找到商品 并且找到商品单价
        Goods goods = goodsService.getGoodsById(goodsId);
        //2.1查询是否有该商品
        int success=0;
        if(cart==null)
        {


            int totalMoney=goods.getPrice()*Integer.parseInt(number);
            //用户编号   数量   商品id   总价 goods 为空
            cart=new Cart(user.getId(),Integer.parseInt(number),Integer.parseInt(goodsId),totalMoney,null);
            success=cartService.add(cart);
        }else {
            //如果有商品就更新
            cart.setBuyCount(cart.getBuyCount()+Integer.parseInt(number));
            cart.setSubtotal(goods.getPrice()*cart.getBuyCount());
            success=cartService.update(cart);
        }
        //2.2
        if(success!=0)
        {
            return new ModelAndView("redirect:/cartSuccess.jsp");
        }
        return  new ModelAndView("redirect:/index.jsp");
    }

    @GetMapping(value = "/getCart", produces = {"application/json;charset=UTF-8"})
    @ResponseBody //序列化--》类型转换  --》jackson ---Json
    public ModelAndView getCart(HttpSession session){
        ModelAndView modelAndView=new ModelAndView("forward:/cart.jsp");
        User user= (User) session.getAttribute("user");
        //判断是否登录
        if(user==null)
        {
            return new ModelAndView("redirect:/register.jsp");
        }
        List<Cart> carts=cartService.findByUid(user.getId());
        modelAndView.addObject("carts",carts);
        return modelAndView;
    }

    @GetMapping(value = "/addCartAjax", produces = {"application/json;charset=UTF-8"})
    @ResponseBody //序列化--》类型转换  --》jackson ---Json
    public ModelAndView  addCartAjax(HttpSession session,@RequestParam int goodsId,@RequestParam int number){
        ModelAndView modelAndView=new ModelAndView("forward:/cart.jsp");
        User user= (User) session.getAttribute("user");
        //判断是否登录
        if(user==null)
        {
            return new ModelAndView("redirect:/login.jsp");
        }
        //查询
        Cart cart=cartService.findByUidAndgid(user.getId(),goodsId);
        if(cart!=null)
        {
            //删除
            if(number==0)
            {
                int num=cartService.delete(user.getId(),goodsId);

            }else{
                //更新
                //获取单价
                int UnitPrice=cart.getSubtotal()/cart.getBuyCount();
                //更新数量
                cart.setBuyCount(cart.getBuyCount()+number);
                //更新金额
                cart.setSubtotal(UnitPrice*cart.getBuyCount());
                cartService.update(cart);

            }
        }

        return modelAndView;
    }


    @GetMapping(value = "/clearCartAjax", produces = {"application/json;charset=UTF-8"})
    @ResponseBody //序列化--》类型转换  --》jackson ---Json
    public int clearCartAjax(HttpSession session){

        User user= (User) session.getAttribute("user");
        //判断是否登录
        if(user==null)
        {
            return -1;
        }
        int num=cartService.deleteByUid(user.getId());
        System.out.println("已删除"+num+"条数据");
        return num;
    }
}

